# -*- coding: utf-8 -*-
# standard
import os
import sys
import re
import decimal

# third

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from functions import *


"""
@Title:   统计 gpu01, hk01 和 hk02 上视频文件的数量
@File: 统计视频.py
@Author: walle 2023年10日24日 17时14分14秒
@Version: 1.0.0
@Desc: 
"""

PORT = 22


def get_gpu_server_videos():
    """
    获取gpu服务器上的所有视频信息。
    """
    command = f'ls -l {SERVER_DEST_DIR} | grep "^-"'
    command2 = f'du {SERVER_DEST_DIR} -h max-depath=1 --block-size=G'  # 查看文件夹大小，单位G
    ssh = paramiko.SSHClient()  
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
    # 连接到远程Linux系统  
    ssh.connect(hostname=SERVER_HOST, port=PORT, username="root", password="Default01!")    
    # 执行命令并获取输出  
    stdin, stdout, stderr = ssh.exec_command(command)  
    output1 = stdout.read().decode()  
    # 执行命令并获取输出  
    stdin, stdout, stderr = ssh.exec_command(command2)  
    output2 = stdout.read().decode() 
    ssh.close()
    if output1 == "":
        good_files, bad_files, size = [], [], 0
    else:
        file_lines = output1.split("\n")
        size = re.match(r"^[0-9,\.]*[a-z,A-Z]", output2).group()
        size = int(size[0: -1])
        print(f"视频总大小： {size}G")
        good_files, bad_files = ScpRecordOrm.format_remote_file_info(file_lines)
    return good_files, bad_files, size


def get_hk01_videos():
    """
    获取爬虫服务器上的所有视频信息。
    """
    list_dir_command = 'ls -l /mnt/projects/download-youtube-clip/downloads/youtube/ | grep "^d"'
    list_dir_command = 'ls -l /mnt/projects/download-youtube-clip/downloads/youtube/'
    ssh = paramiko.SSHClient()  
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
    # 连接到远程Linux系统  
    ssh.connect(hostname="47.242.28.132", port=PORT, username="spider", password="jingying")    
    # 执行命令并获取输出  
    stdin, stdout, stderr = ssh.exec_command(list_dir_command)  
    output = stdout.read().decode()  
    
    folder_lines = output.split("\n")
    folder_names = []
    for folder_line in folder_lines:
        words = [y for y in [x.strip() for x in folder_line.split(" ")] if y != ""]
        if len(words) < 9:
            continue
        folder_name = words[8]
        folder_names.append(folder_name)

    total_good_files, total_bad_files = [], []
    for folder_name in folder_names:
        command = f'ls -l /mnt/projects/download-youtube-clip/downloads/youtube/{folder_name} | grep "^-"'
        # 执行命令并获取输出  
        stdin, stdout, stderr = ssh.exec_command(command)  
        output = stdout.read().decode()  
        file_lines = output.split("\n")
        good_files, bad_files = ScpRecordOrm.format_remote_file_info(file_lines)
        total_good_files.extend(good_files)
        total_bad_files.extend(bad_files)
    ssh.close()
    return total_good_files, total_bad_files


def get_hk02_videos():
    """
    获取爬虫服务器上的所有视频信息。
    """
    command = 'ls -l /mnt/projects/work_data/ | grep "^-"'
    ssh = paramiko.SSHClient()  
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
    # 连接到远程Linux系统  
    ssh.connect(hostname="47.243.81.213", port=PORT, username="spider", password="jingying")    
    # 执行命令并获取输出  
    stdin, stdout, stderr = ssh.exec_command(command)  
    output = stdout.read().decode()  
    ssh.close()
    file_lines = output.split("\n")
    good_files, bad_files = ScpRecordOrm.format_remote_file_info(file_lines)
    return good_files, bad_files


def main():
    key = f"上次统计的时间"
    value = FileCacheManager.get(key=key)
    prev_time, prev_total, prev_size = (None, None, 0) if value is None else value
    gpu1_good_files, _gpu1_bad_files, gpu1_size = get_gpu_server_videos()
    gpu1_count = len(gpu1_good_files)
    print(f"gpu1 上有视频 {gpu1_count} 个")
    hk01_good_files = get_hk01_videos()[0]
    hk01_count = len(hk01_good_files)
    print(f"hk01 上有视频 {hk01_count} 个")
    hk02_good_files = get_hk02_videos()[0]
    hk02_count = len(hk02_good_files)
    print(f"hk02 上有视频 {hk02_count} 个")
    total_count = gpu1_count + hk01_count + hk02_count
    end_time = datetime.now()
    print(f"总共有视频 {total_count} 个")
    FileCacheManager.set(key=key, value=(end_time, total_count, gpu1_size), timeout=0)
    if prev_time:
        hour = (end_time - prev_time).total_seconds() / 3600
        download_num_speed = (total_count - prev_total) / hour
        download_num_speed = decimal.Decimal(download_num_speed).quantize(decimal.Decimal('0.00')).to_eng_string()
        download_size_speed = (gpu1_size - prev_size) / hour
        download_size_speed = decimal.Decimal(download_size_speed).quantize(decimal.Decimal('0.00')).to_eng_string()
        print(f"当前下载速度 {download_num_speed} 个/小时,({download_size_speed} G/小时)")

    


if __name__ == '__main__':
    main()
    pass